package xyz.robinbingo.leetcode.a20.b05.c07;

/**
 * Solution
 *
 * @author Robin
 * @date 2020/5/7 20:44
 **/
public class Solution {
    public static boolean isSubtree(TreeNode s, TreeNode t) {
        if (t == null) {
            return true;
        }
        if (s == null) {
            return false;
        }

        return equals(s, t) || isSubtree(s.left, t) || isSubtree(s.right, t);
    }

    private static boolean equals(TreeNode s, TreeNode t) {
        if (s == null || t == null) {
            return s == null && t == null;
        }
        return s.val == t.val && equals(s.left, t.left) && equals(s.right, t.right);
    }
}